package leetcode.题库.二叉树;


import leetcode.题库.pubclass.TreeNode;

/**
 * description:
 *
 * @author 颜真明
 * @date 2024/3/28  9:37
 */
public class Solution654 {

    public TreeNode constructMaximumBinaryTree(int[] nums) {
        return constructMaximumBinaryTree(nums, 0, nums.length - 1);
    }

    public TreeNode constructMaximumBinaryTree(int[] nums, int left, int right) {
        if (left > right) {
            return null;
        }
        int max = Integer.MIN_VALUE;
        int mid = left;
        for (int i = left; i <= right; i++) {
            if (nums[i] > max) {
                max = nums[i];
                mid = i;
            }
        }
        TreeNode tmp = new TreeNode(nums[mid]);
        tmp.left = constructMaximumBinaryTree(nums, left, mid-1);
        tmp.right = constructMaximumBinaryTree(nums, mid+1, right);
        return tmp;
    }

}
